package com.itcast.demo_one.kotlin

fun main(){
    val man = Person1("张三", 18)
    man.show()
    val woman = Person2("里斯", 20)
    woman.show()
}

/**
 * 显示主构造函数，包含constructor
 */
class Person1 constructor(var name: String, var age: Int) {
    // init代码块
    // 实例化对象成功后如果有init，一定会执行init
    // 作用：初始化对象，对参数进行类型校验
    init {
        println("来了来了")
        require(age > 0){"年龄必须大于0"}
        require(name.isNotEmpty()){"姓名不能为空"}
    }
    fun show(){
        println("name: $name, age: $age")
    }
}
/**
 * 隐式主构造函数，不用写constructor
 */
class Person2(var name: String, var age: Int) {
    init {
        println("我又回来了")
        require(age > 0){"年龄必须大于0"}
        require(name.isNotEmpty()){"姓名不能为空"}
    }
    fun show(){
        println("name: $name, age: $age")
    }
}